using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class JobSeekers_copyletter : System.Web.UI.Page
{
    string connectionString = "";
    string letterID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
             WebConfigurationManager.ConnectionStrings[
               "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            letterID = Request.QueryString.Get("letterid");
            if (!IsPostBack)
            {

                literalLetterTitle.Text =  
                Request.QueryString.Get("title");
                txtTitle.Text = "Copy of " + 
                literalLetterTitle.Text;
                CheckLetter();
            }
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = "SPCopyLetter";
            cls.CommandType = CommandType.StoredProcedure;
            cls.ParameterCollection = new 
            string[3] { "@UserID", 
            "@LetterID", "@Title" };
            cls.ValueCollection = new 
            string[3]{Convert.ToString(
            Session.Contents["jsuserid"]), 
            letterID, txtTitle.Text};
            if (cls.ExecuteNonQuery() > 0)
                Response.Redirect("coverletters.aspx");
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    void CheckLetter()
    {
        try
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = 
            "select  * from tblJSCoverLetters" +
             "  where UserID=@UserID";
            cls.ParameterCollection = new 
            string[1] { "@UserID" };
            cls.ValueCollection = new 
            string[1]{Convert.ToString(
            Session.Contents["jsuserid"])};
            
            int records = Convert.ToInt32(
            cls.ExecuteScalar());
            if (records >= 5)
                btnNew.Enabled = false;
            else
                btnNew.Enabled = true;
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
}
